package com.cooper.service.seller;

import java.util.Collections;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cooper.dao.seller.DesignerDAO;
import com.cooper.entity.DesignerEntity;
import com.cooper.entity.ShopStatEntity;
import com.cooper.pagination.PageInfo;
import com.cooper.pagination.PageResult;
import com.cooper.vo.CompositeDesignerProjectVo;
import com.cooper.vo.DesignerSearchVo;
import com.cooper.vo.DesignerVo;

@Service
public class DesignerService {
	
	protected Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private ShopStatService shopStatService;
	
	@Autowired
	private DesignerDAO designerDao;
	
	public Long createDesigner(DesignerEntity designer){
		Long id = 0L;
		try {
			id = designerDao.addDesigner(designer);
			
			ShopStatEntity shopStat = new ShopStatEntity();
			shopStat.setSellerId(designer.getUserId());
			this.shopStatService.saveEntity(shopStat);
		} catch (Exception e) {
			logger.error("DesignerService-createDesigner-error", e);
		}
		return id;
	}
	
	public void updateDesigner(DesignerEntity designer){
		try {
			designerDao.updateDesigner(designer);
		} catch (Exception e) {
			logger.error("DesignerService-updateDesigner-error", e);
		}
	}
	
	public DesignerEntity findDesignerById(Long id){
		try {
			return designerDao.getDesigner(id);
		} catch (Exception e) {
			logger.error("DesignerService-findDesignerById-error", e);
		}
		return null;
	}
	
	public DesignerEntity findDesignerByUserId(Long userId){
		try {
			return designerDao.getDesignerByUserId(userId);
		} catch (Exception e) {
			logger.error("DesignerService-findDesignerByUserId-error", e);
		}
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public PageResult<CompositeDesignerProjectVo> findCompositeDesignerProjects(CompositeDesignerProjectVo queryParamVo, PageInfo pageInfo){
		try {
			int rowCount = designerDao.queryDesignersCount(queryParamVo);
			pageInfo.setRowCount(rowCount);
			if(0 == rowCount){
				return new PageResult<CompositeDesignerProjectVo>(Collections.EMPTY_LIST, pageInfo);
			}
			
			List<CompositeDesignerProjectVo> dataList = designerDao.queryDesigners(queryParamVo, pageInfo);
			return new PageResult<CompositeDesignerProjectVo>(dataList, pageInfo);
		} catch (Exception e) {
			logger.error("DesignerService-findCompositeDesignerProjects-error",e);
		} 
	    return new PageResult<CompositeDesignerProjectVo>(Collections.EMPTY_LIST, pageInfo);
	}
	
	@SuppressWarnings("unchecked")
	public PageResult<DesignerVo> findHotShops(String sellerType,PageInfo pageInfo){
		try {
			List<DesignerVo> dataList = designerDao.queryHostShops(sellerType, pageInfo);
			pageInfo.setRowCount(dataList.size());
			
			return new PageResult<DesignerVo>(dataList, pageInfo);
		} catch (Exception e) {
			logger.error("DesignerService-findHotShops-error",e);
		} 
	    return new PageResult<DesignerVo>(Collections.EMPTY_LIST, pageInfo);
	}
	
	@SuppressWarnings("unchecked")
	public PageResult<DesignerVo> queryForList(DesignerSearchVo searchVo, PageInfo pageInfo) throws Exception{
		int rowCount = this.designerDao.queryDesignerCount(searchVo);
		pageInfo.setRowCount(rowCount);
		
		if(0 == rowCount){
			return new PageResult<DesignerVo>(Collections.EMPTY_LIST, pageInfo);
		}
		
		List<DesignerVo> dataList = this.designerDao.queryForList(searchVo, pageInfo);
		return new PageResult<DesignerVo>(dataList, pageInfo);
	}
	
}
